﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 学科仓储
/// </summary>
public class MdSubjectRep : BaseRep<MdSubjectModel>
{
    /// <summary>
    /// 通过编号获取学科信息
    /// </summary>
    /// <param name="subjectId">学科编号</param>
    /// <returns>MdSubjectModel</returns>
    public MdSubjectModel GetMdSubjectBySubjectId(long? subjectId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `subject_id`
                ,`subject_name`
                ,`parent_id`
                ,`subject_sort`
                ,`status`
                ,`del_flag`
                ,`create_by`
                ,`create_time`
                ,`update_by`
                ,`update_time`
                ,`remark`
            from `md_subject`
            where 1 = 1 and `subject_id` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdSubjectModel>(sqlStr, subjectId);
    }

    /// <summary>
    /// 通过学科名称获取学科信息
    /// </summary>
    /// <param name="subjectName">学科名称</param>
    /// <returns>MdSubjectModel</returns>
    public MdSubjectModel GetMdSubjectBySubjectName(string subjectName)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `subject_id`
                ,`subject_name`
                ,`parent_id`
                ,`subject_sort`
                ,`status`
                ,`del_flag`
                ,`create_by`
                ,`create_time`
                ,`update_by`
                ,`update_time`
                ,`remark`
            from `md_subject`
            where 1 = 1 and `subject_name` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<MdSubjectModel>(sqlStr, subjectName);
    }
    /// <summary>
    /// 获取学科列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="model">筛选条件</param>
    /// <returns>IEnumerable<MdSubjectModel></returns>
    public IEnumerable<MdSubjectModel> GetMdSubjectList(string? orderBy, bool? isDesc, MdSubjectModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.SubjectId > 0)
        {
            sqlWhere += " and subject_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.SubjectName))
        {
            sqlWhere += " and subject_name like @1 ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `subject_id`
                ,`subject_name`
                ,`parent_id`
                ,`subject_sort`
                ,`status`
                ,`del_flag`
                ,`create_by`
                ,`create_time`
                ,`update_by`
                ,`update_time`
                ,`remark`
            from `md_subject`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<MdSubjectModel>(
            sqlStr,
            model.SubjectId,
            string.Format(@"%{0}%", model.SubjectName),
            model.ParentId,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 分页获取学科列表数据
    /// </summary>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="model">筛选条件</param>
    /// <returns>Page<MdSubjectModel></returns>
    public Page<MdSubjectModel> GetMdSubjectPageList(string? orderBy, bool? isDesc, int pageIndex, int pageSize, MdSubjectModel model)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.SubjectId > 0)
        {
            sqlWhere += " and subject_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.SubjectName))
        {
            sqlWhere += " and subject_name like @1 ";
        }
        if (model.ParentId > -1)
        {
            sqlWhere += " and parent_id = @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and status = @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.DelFlag))
        {
            sqlWhere += " and del_flag = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `subject_id`
                ,`subject_name`
                ,`parent_id`
                ,`subject_sort`
                ,`status`
                ,`del_flag`
                ,`create_by`
                ,`create_time`
                ,`update_by`
                ,`update_time`
                ,`remark`
            from `md_subject`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<MdSubjectModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.SubjectId,
            string.Format(@"%{0}%", model.SubjectName),
            model.ParentId,
            model.Status,
            model.DelFlag,
            model.CreateBy,
            model.UpdateBy);
    }
    /// <summary>
    /// 通过编号更新学科信息
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateMdSubjectBySubjectId(MdSubjectModel model)
    {
        var setList = new List<string>();

        #region ===set===
        setList.Add(" subject_name = @2 ");

        setList.Add(" subject_sort = @3 ");

        setList.Add(" parent_id = @4 ");

        setList.Add(" status = @5 ");

        setList.Add(" update_by = @6 ");

        setList.Add(" update_time = @7 ");

        setList.Add(" remark = @8 ");
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
                set {0}
                where 1 = 1 and del_flag = @0 and subject_id = @1
         ", string.Join(",", setList));
        #endregion

        var res = MdTrainingEducationDB.GetInstance().Update<MdSubjectModel>(
            sqlStr,
            model.DelFlag,
            model.SubjectId,
            model.SubjectName,
            model.SubjectSort,
            model.ParentId,
            model.Status,
            model.UpdateBy,
            model.UpdateTime,
            model.Remark
         );

        return res;
    }
}

